package org.yeruyi.common.util;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by yeruyi on 2017/2/2.
 * 2017年02月02日 创建此 工具类
 */

public class StringUtil {

    private static final String TAG = "[StringUtil]";

    /**
     * 手机号判断是否合法
     *
     * @param phone 手机号待检测
     * @return true or false
     */
    public static boolean devidePhone(String phone) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(phone);
        return m.matches();
    }

    /**
     * 格式化cookie
     *
     * @param cookie 原始Cookie
     * @return cookie
     */
    public static String formalCookie(String cookie) {
        String[] tmp = cookie.split(";");
        return tmp[0] + ";";
    }

    /**
     * 判断密码是否合法
     *
     * @param password 原始密码
     * @return boolean
     */
    public static boolean devidePassword(String password) {
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
        if (password.length() < 8 || password.length() > 16) {
            return false;
        } else {
            return password.matches(regex);
        }
    }

    /**
     * 单位数增加0
     * @param a or
     * @return
     */
    public static String formalInt(int a) {
        String tmp = "" + a;
        if (tmp.length() == 1) {
            return "0" + tmp;
        }
        return tmp;
    }
}
